import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'

// 1.安装插件
Vue.use(Vuex)

// 2.创建Store对象
const store = new Vuex.Store({
  state:{
    cartList:[]
  },
  mutations:{
    addCart(state,payload){
      // state.cartList.push(payload){
        // 1.查找之前数组中是否有该商品
        let oldProduct = state.cartList.find(item => item.iid === payload.iid);

        if(oldProduct){
          oldProduct.count +=1
        }
        else{
          payload.count = 1;
          state.cartList.push(payload)
        }
      }
  },
  getters

})

// 3.挂载Vue实例上
export default store